/*
 * To change this license header, choose License Headers in Project Properties. To change this
 * template file, choose Tools | Templates and open the template in the editor.
 */
package chalmers.game.subterranean.ui.view;

import chalmers.game.subterranean.event.Bus;
import chalmers.game.subterranean.event.events.RestartGameEvent;
import chalmers.game.subterranean.event.events.player.SetTorchSizeEvent;
import chalmers.game.subterranean.util.options.PlayerValues;
import chalmers.game.subterranean.util.options.WorldValues;
import com.google.common.eventbus.Subscribe;
import javafx.scene.Group;
import javafx.scene.control.ProgressBar;

/**
 * 
 * @author TH
 */
public class HUD {

  public final Group view;
  private final ProgressBar bar;

  public HUD() {
    view = new Group();
    bar = new ProgressBar();
    initBar();
    view.getChildren().add(bar);
    Bus.INSTANCE.register(this);
  }

  private void initBar() {
    int xS = 400;
    int yS = 50;
    int padding = 5;

    bar.setProgress(1);
    bar.setPrefSize(xS, yS);
    bar.setLayoutX(WorldValues.GAME_RESOLUTION_WIDTH - xS - padding);
    bar.setLayoutY(padding);

  }

  @Subscribe
  public void handleSetTorchSize(SetTorchSizeEvent evt) {
    Double newVal = (1.0 / PlayerValues.TORCH_DEFAULT_RADIUS) * evt.radius;
    Animate.anim(bar.progressProperty(), newVal, PlayerValues.TORCH_UPDATE_INTERVAL * 1000);
  }

  @Subscribe
  public void handleRestartGame(RestartGameEvent evt) {
    Animate.anim(view.opacityProperty(), 0.0, PlayerValues.TORCH_UPDATE_INTERVAL * 1000);
    Bus.INSTANCE.unRegister(this);
  }
}
